package com.fndsoft.zheshangmeap.handler;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;

import com.fndsoft.zheshangmeap.R;
import com.fndsoft.zheshangmeap.ui.adapter.ListDialogAdapter;
import com.fndsoft.zheshangmeap.ui.base.BaseAc;
import com.fndsoft.zheshangmeap.util.DateUtil;
import com.fndsoft.zheshangmeap.util.ValueUtil;
import com.fndsoft.zheshangmeap.util.ViewUtil;
import com.fndsoft.zheshangmeap.view.MeapDialog;

/**
 * @author carlos carlosk@163.com
 * @version 创建时间：2014年3月3日 上午11:17:29 弹出框的辅助类
 */

public class DialogHandler extends BaseHandler {

	public static final int SINGLE_INPUT_TEXT_TYPE = 0;// 单行输入文字内容的弹出框类型
	public static final int SINGLE_INPUT_NUMBER_TYPE = 1;// 单行输入数字内容的弹出框类型
	public static final int SIMPLE_CONFIG_TYPE = 3;// 单一确认弹出框类型
	public static final int TWO_BTN_CONFIG_TYPE = 7;// 两个按钮确认弹出框类型
	public static final int MULROW_SINGLE_CHOOSE_TYPE = 4;// 显示多行单选的弹出框类型
	public static final int DATE_TYPE = 5;// 显示日期的弹出框类型
	public static final int TIME_TYPE = 6;// 显示时间的弹出框类型
	private static MeapDialog meapAlertDialog;
	private static DatePickerDialog mDatePickerDialog;
	private static TimePickerDialog timePickerDialog;
	private static View.OnClickListener cancelClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			dismissDialog();
			if (null == mMeapDialogCancelInterface) {
				return;
			}
			mMeapDialogCancelInterface.onClickCancelBtn();
		}
	};
	private static View.OnClickListener confirmClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			dismissDialog();
			if (null == mMeapDialogInterface) {
				return;
			}
			switch (meapDialogType) {
			case SIMPLE_CONFIG_TYPE:
			case TWO_BTN_CONFIG_TYPE:
				mMeapDialogInterface.onClickConfirmBtn();
				break;
			case SINGLE_INPUT_TEXT_TYPE:
				mMeapDialogInterface.onClickConfirmBtn(inputDialogEditText
						.getText().toString());
				break;
			case SINGLE_INPUT_NUMBER_TYPE:
				String text = inputDialogEditText.getText().toString();
				try {
					int number = Integer.parseInt(text);
					mMeapDialogInterface.onClickConfirmBtn(number);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
				break;
			case DATE_TYPE: {
				Date seletedDate = DateUtil.formatDate(datePicker.getYear(),
						datePicker.getMonth(), datePicker.getDayOfMonth(), -1,
						-1);
				mMeapDialogInterface.onSetDate(seletedDate);
			}
				break;
			case TIME_TYPE: {
				Date seletedDate = DateUtil.formatDate(-1, -1, -1,
						timePicker.getCurrentHour(),
						timePicker.getCurrentMinute());
				mMeapDialogInterface.onSetDate(seletedDate);
			}
				break;

			default:
				break;
			}
		}
	};

	private static MeapDialogListenerImpl mMeapDialogInterface = null;
	private static MeapDialogListenerImpl mMeapDialogCancelInterface = null;
	private static int meapDialogType;// 弹出框的类型
	private static EditText inputDialogEditText;// 弹出框的输入框

	/**
	 * 显示多行单选的弹出框
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年3月3日 上午11:36:33
	 * @param title
	 * @param data
	 */
	public static void showMulRowSingleChooseDialog(String title,
			String[] data, MeapDialogListenerImpl meapDialogInterface) {
		showMulRowSingleChooseDialog(title, data, meapDialogInterface, -1);
	}

	public static void showMulRowSingleChooseDialog(String title,
			String[] data, MeapDialogListenerImpl meapDialogInterface,
			int seletedIndex) {
		meapDialogType = MULROW_SINGLE_CHOOSE_TYPE;
		dismissDialog();
		mMeapDialogInterface = meapDialogInterface;
		View view = ViewUtil.buildView(R.layout.list_dialog);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
		tvTitle.setText(title);
		ListView lvList = (ListView) view.findViewById(R.id.lvList);
		List<String> dataList = Arrays.asList(data);
		ListDialogAdapter adapter = new ListDialogAdapter(dataList);
		adapter.notifyDataSetChanged(seletedIndex);
		lvList.setAdapter(adapter);
		lvList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				dismissDialog();
				if (meapDialogType == MULROW_SINGLE_CHOOSE_TYPE) {
					mMeapDialogInterface.onClickSeletedIndex(arg2);
				}
			}
		});
		ImageView ImageView = (ImageView) view.findViewById(R.id.ivNo);
		ImageView.setOnClickListener(cancelClickListener);

		MeapDialog dialog = new MeapDialog(view, BaseAc.currentActivity);
		meapAlertDialog = dialog;
		dialog.show();
	}

	/**
	 * 显示输入框的弹出框
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年3月3日 上午9:36:22
	 * @param type
	 *            0为文本内容,1为数字内容
	 * @param hintString
	 *            隐藏字
	 */
	public static void showEditContentRowDialog(String title,
			String hintString, MeapDialogListenerImpl meapDialogInterface) {

		dismissDialog();
		meapDialogType = SINGLE_INPUT_TEXT_TYPE;
		mMeapDialogInterface = meapDialogInterface;

		View view = ViewUtil.buildView(R.layout.edit_dialog);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
		if (ValueUtil.isStrEmpty(title)) {
			title = ValueUtil.getString(R.string.app_name);
		}
		tvTitle.setText(title);
		EditText etContent = (EditText) view.findViewById(R.id.etContent);
		if (ValueUtil.isStrNotEmpty(hintString)) {
			etContent.setHint(hintString);
		}
		inputDialogEditText = etContent;
		final Button comfirBtn = (Button) view.findViewById(R.id.btnRight);
		ViewUtil.setViewsGone(comfirBtn);
		etContent.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if (arg0.length() > 0) {
					ViewUtil.setViewsVisible(comfirBtn);
				} else {
					ViewUtil.setViewsGone(comfirBtn);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
		View leftBtn = view.findViewById(R.id.btnLeft);
		leftBtn.setOnClickListener(cancelClickListener);
		comfirBtn.setOnClickListener(confirmClickListener);
		MeapDialog dialog = new MeapDialog(view, BaseAc.currentActivity);
		dialog.show();
		meapAlertDialog = dialog;
	}

	/**
	 * 选择时间弹出框
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年3月3日 下午5:55:34
	 * @param title
	 * @param meapDialogInterface
	 */
	public static void showTimeDialog(MeapDialogListenerImpl meapDialogInterface) {
		showTimeDialog(meapDialogInterface, null);
	}

	/**
	 * 显示时间的弹出框
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：Mar 9, 2014 11:36:42 AM
	 * @param title
	 * @param meapDialogInterface
	 * @param defaultDate
	 */
	public static void showTimeDialog(
			MeapDialogListenerImpl meapDialogInterface, Date defaultDate) {
		dismissDialog();
		meapDialogType = TIME_TYPE;
		mMeapDialogInterface = meapDialogInterface;
		if (null == defaultDate) {
			defaultDate = new Date();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(defaultDate);

		View view = ViewUtil.buildView(R.layout.time_dialog);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
		View btnLeft = view.findViewById(R.id.btnLeft);
		btnLeft.setOnClickListener(cancelClickListener);
		View btnRight = view.findViewById(R.id.btnRight);
		btnRight.setOnClickListener(confirmClickListener);
		timePicker = (TimePicker) view.findViewById(R.id.tpDate);
		timePicker.setIs24HourView(true);
		timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
		timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
		tvTitle.setText(R.string.time_dialog_title);
		MeapDialog dialog = new MeapDialog(view, BaseAc.currentActivity);
		meapAlertDialog = dialog;
		dialog.show();

	}

	/**
	 * 选择日期的
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年3月3日 下午2:04:11
	 * @param title
	 * @param meapDialogInterface
	 */
	public static void showDateDialog(MeapDialogListenerImpl meapDialogInterface) {
		showDateDialog(meapDialogInterface, new Date());
	}

	private static DatePicker datePicker;
	private static TimePicker timePicker;

	/**
	 * 显示日期弹出框
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：Mar 9, 2014 1:04:19 AM
	 * @param title
	 * @param meapDialogInterface
	 * @param defaultDate
	 */
	public static void showDateDialog(
			MeapDialogListenerImpl meapDialogInterface, Date defaultDate) {

		dismissDialog();
		meapDialogType = DATE_TYPE;
		mMeapDialogInterface = meapDialogInterface;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(defaultDate);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);

		View view = ViewUtil.buildView(R.layout.date_dialog);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
		View btnLeft = view.findViewById(R.id.btnLeft);
		btnLeft.setOnClickListener(cancelClickListener);
		View btnRight = view.findViewById(R.id.btnRight);
		btnRight.setOnClickListener(confirmClickListener);
		datePicker = (DatePicker) view.findViewById(R.id.dpDate);
		datePicker.init(year, month, day, null);
		tvTitle.setText(R.string.date_dialog_title);

		MeapDialog dialog = new MeapDialog(view, BaseAc.currentActivity);
		meapAlertDialog = dialog;
		dialog.show();
	}

	/**
	 * 显示单一弹出框,有取消和确认按钮
	 */
	public static void showAlertTwoBtnDialog(String title, String msg,
			MeapDialogListenerImpl meapDialogInterface) {
		dismissDialog();

		meapDialogType = TWO_BTN_CONFIG_TYPE;
		mMeapDialogInterface = meapDialogInterface;

		View view = ViewUtil.buildView(R.layout.alert_dialog);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
		if (ValueUtil.isStrEmpty(title)) {
			title = ValueUtil.getString(R.string.app_name);
		}
		tvTitle.setText(title);
		TextView tvContent = (TextView) view.findViewById(R.id.tvContent);
		tvContent.setText(msg);
		Button comfirBtn = (Button) view.findViewById(R.id.btnRight);
		View leftBtn = view.findViewById(R.id.btnLeft);
		leftBtn.setOnClickListener(cancelClickListener);
		comfirBtn.setOnClickListener(confirmClickListener);
		MeapDialog dialog = new MeapDialog(view, BaseAc.currentActivity);
		dialog.show();
		meapAlertDialog = dialog;

	}

	public static void showAlertTwoBtnDialog(String title, String msg,
			MeapDialogListenerImpl confirmMeapDialogInterface,
			MeapDialogListenerImpl cancelMeapDialogInterface) {
		dismissDialog();

		meapDialogType = TWO_BTN_CONFIG_TYPE;
		mMeapDialogInterface = confirmMeapDialogInterface;
		mMeapDialogCancelInterface = cancelMeapDialogInterface;

		View view = ViewUtil.buildView(R.layout.alert_dialog);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
		if (ValueUtil.isStrEmpty(title)) {
			title = ValueUtil.getString(R.string.app_name);
		}
		tvTitle.setText(title);
		TextView tvContent = (TextView) view.findViewById(R.id.tvContent);
		tvContent.setText(msg);
		Button comfirBtn = (Button) view.findViewById(R.id.btnRight);
		View leftBtn = view.findViewById(R.id.btnLeft);
		leftBtn.setOnClickListener(cancelClickListener);
		comfirBtn.setOnClickListener(confirmClickListener);
		MeapDialog dialog = new MeapDialog(view, BaseAc.currentActivity);
		dialog.show();
		meapAlertDialog = dialog;

	}

	/**
	 * 显示单一弹出框
	 */
	public static void showAlertDialog(String title, String msg,
			MeapDialogListenerImpl meapDialogInterface) {
		dismissDialog();

		meapDialogType = SIMPLE_CONFIG_TYPE;
		mMeapDialogInterface = meapDialogInterface;

		View view = ViewUtil.buildView(R.layout.alert_dialog);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
		tvTitle.setText(title);
		TextView tvContent = (TextView) view.findViewById(R.id.tvContent);
		tvContent.setText(msg);
		Button comfirBtn = (Button) view.findViewById(R.id.btnRight);
		View leftBtn = view.findViewById(R.id.btnLeft);
		// 隐藏左边按钮和中间的分割线
		View vBtnsDiver = view.findViewById(R.id.vBtnsDiver);
		ViewUtil.setViewsGone(leftBtn, vBtnsDiver);
		comfirBtn.setOnClickListener(confirmClickListener);
		MeapDialog dialog = new MeapDialog(view, BaseAc.currentActivity);
		dialog.show();
		meapAlertDialog = dialog;
	}

	/**
	 * 隐藏弹出框
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年3月3日 上午11:07:20
	 */
	public static void dismissDialog() {
		if (null != meapAlertDialog) {
			meapAlertDialog.dismiss();
			meapAlertDialog = null;
		}
		if (null != mDatePickerDialog) {
			mDatePickerDialog.dismiss();
			mDatePickerDialog = null;
		}
		if (null != timePickerDialog) {
			timePickerDialog.dismiss();
			timePickerDialog = null;
		}
	}

	private static interface MeapDialogInterface {
		/**
		 * 如果是输入框则返回到editTextString
		 * 
		 * @author carlos carlosk@163.com
		 * @version 创建时间：2014年3月3日 上午11:09:13
		 */
		public void onClickConfirmBtn(String editTextString);

		/**
		 * 返回选中的内容为数组
		 * 
		 * @author carlos carlosk@163.com
		 * @version 创建时间：2014年3月3日 上午11:09:13
		 */
		public void onClickConfirmBtn(int number);

		public void onClickConfirmBtn();

		/**
		 * 返回多选的row index
		 * 
		 * @author carlos carlosk@163.com
		 * @version 创建时间：2014年3月3日 上午11:09:13
		 */
		public void onClickSeletedIndex(int seletedIndex);

		/**
		 * 返回设置的日期
		 * 
		 * @author carlos carlosk@163.com
		 * @version 创建时间：2014年3月3日 下午2:07:57
		 * @param date
		 */
		public void onSetDate(Date date);

	}

	public static class MeapDialogListenerImpl implements MeapDialogInterface {

		@Override
		public void onClickConfirmBtn(String editTextString) {

		}

		@Override
		public void onClickConfirmBtn(int number) {

		}

		@Override
		public void onClickSeletedIndex(int seletedIndex) {

		}

		@Override
		public void onSetDate(Date date) {

		}

		@Override
		public void onClickConfirmBtn() {

		}

		public void onClickCancelBtn() {

		}
	}
}
